package kaoshi.dao.impl;

import kaoshi.dao.BaseDao;
import kaoshi.dao.personDao;
import kaoshi.entity.person;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class personDaoImpl extends BaseDao implements personDao {
    @Override
    public List<person> selAll() {
        List<person> list = new ArrayList<>();
        try {
            String sql = "SELECT * FROM person";
            rs=super.executeQuery(sql,null);
            while (rs.next()){
                person p = new person();
                p.setId(rs.getInt(1));
                p.setName(rs.getString(2));
                p.setSex(rs.getString(3));
                p.setAge(rs.getInt(4));
                p.setRank(rs.getString(5));
                p.setBumeng(rs.getString(6));
                list.add(p);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int delID(int id) {
        int count = 0;
        try {
            String sql = "delete from person where id=?";
            Object[] param = {id};
            count = super.executeUpdate(sql,param);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public int insert(person person) {
        int count = 0;
        try {
            String sql = "insert into person(name,sex,age,rank,department)VALUES(?,?,?,?,?)";
            Object[] param = {person.getName(),person.getSex(),person.getAge(),person.getRank(),person.getBumeng()};
            count = super.executeUpdate(sql,param);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public int update(person person) {
        int count=0;
        try {
            String sql = "update person set name=?,sex=?,age=?,rank=?,department=? where id=?";
            Object[] params = {person.getName(),person.getSex(),person.getAge(),person.getRank(),person.getBumeng(),person.getId()};
            count = super.executeUpdate(sql,params);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public List<person> getByPage(int pageNo, int pageSize) {
        List<person> list = new ArrayList<>();
        try {
            String sql = "SELECT * FROM person limit ?,?";
            Object[] params={(pageNo-1)*pageSize,pageSize};
            rs=super.executeQuery(sql,params);
            while (rs.next()){
                person p = new person();
                p.setId(rs.getInt(1));
                p.setName(rs.getString(2));
                p.setSex(rs.getString(3));
                p.setAge(rs.getInt(4));
                p.setRank(rs.getString(5));
                p.setBumeng(rs.getString(6));
                list.add(p);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int getByPaged() {
        String sql = "select count(1) from person";
        ResultSet rs = null;
        int count = 0;
        try {
            rs = super.executeQuery(sql,null);
            if (rs.next()){
                count = rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            super.closeAll(rs,pstmt, conn);
        }
        return count;
    }

    @Override
    public person inquireMaxId() {
        person person=null;
        try{
            String sql="select * from person where id=(select max(id) from person)";
            rs=super.executeQuery(sql,null);
            if (rs.next()){
                person=new person();
                person.setId(rs.getInt(1));
                person.setName(rs.getString(2));
                person.setSex(rs.getString(3));
                person.setAge(rs.getInt(4));
                person.setRank(rs.getString(5));
                person.setBumeng(rs.getString(6));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return person;
    }
}
